package com.zengguang.testkaoshi.services.impl;

import com.zengguang.testkaoshi.dao.RoleDao;
import com.zengguang.testkaoshi.dao.RolePersonAuthDao;
import com.zengguang.testkaoshi.eo.RoleEOPage;
import com.zengguang.testkaoshi.pojo.Role;
import com.zengguang.testkaoshi.services.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService implements IRoleService {
    @Autowired
    private RoleDao dao;
    @Autowired
    private RolePersonAuthDao rolePersonAuthDao;

    @Override
    public int insertRole(Role role) {
        int returnInt=0;
        //1.判断角色名不能重复
        int roleNameCount=dao.getRoleNameCount(role.getRolename());
        if (roleNameCount<1){
            //2.执行插入操作
            int insertInt=dao.insertRole(role);
            //3.赋值
            returnInt=insertInt;
        }else {
            returnInt=-1;//表示有相同名称
        }

        return returnInt;
    }

    @Override
    public int updateRole(Role role) {
        int isUpdate=0;
        try {
            isUpdate=dao.updateRole(role);
        }catch (Exception ex){
            isUpdate=-1;
            //打印更新时报错的情况
            System.out.println("更新角色信息时执行SQL报错的原因为："+ex.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int deleteRole(int roleid) {
        int isdel=0;
        /*业务流程判断*/
        //(1)删除角色不能与角色人员权限表有关联
        try {
            //返回的结果总数量大于0说明有这个角色才可以删除
            if (rolePersonAuthDao.getRoleCount(roleid)>0){
                isdel=dao.deleteRole(roleid);
            }else {
                isdel=-1;
            }
        }catch (Exception ex){
            isdel=-1;
            System.out.println("删除角色信息执行SQL报错"+ex.getMessage());
        }

        return isdel;
    }

    @Override
    public List<RoleEOPage> getWhereAll(RoleEOPage roleEOPage) {
        return dao.getWhereAll(roleEOPage);
    }
}
